﻿@using ST = EmoneyWeiXinCommon.StringTools;
@{
    Layout = "~/Views/Shared/_Layout.cshtml";
    ViewBag.Title = "二维码参数管理";
}

<div class="right">
    <h3>二维码参数管理</h3>
    <!--manage-->
    <div class="manage">
        <!--manage_top-->
        <div class="manage_top">
            <ul class="manage_top_ul">
            </ul>
        </div>
        <!--manage_top end-->
        <p class="manage_line"></p>
        <div class="manage_register">
            <p><span>分类：</span><input id="name" type="text"><input style="display: none" id="hidid" type="text"></p>
            <p>
                <span>参数区间：</span>
                <input id="begin_val" type="text" class="manage_register_text"><span class="manage_register_font">至</span><input id="end_val" type="text" class="manage_register_text">
            </p>
        </div>
        <div class="manage_register">
            <p class="manage_register_p"><span>备注：</span><input id="memo" type="text"></p>
        </div>
        <p class="manage_add">
            <a id="btnSave" style='display:@(new EmoneyWeiXin.Controllers.BaseConfigurationController().DisplayOperate(2, 3,ViewBag.leftid))'>保存</a>
            <a id="btnRet" class="manage_add_font">取消</a>
        </p>

        <!--manage_top end-->
        <p class="manage_line"></p>
        <!--manage_bottom-->
        <div class="manage_bottom">
            <table border="0" cellpadding="0" cellspacing="0" width="100%">
                <tr>
                    <th width="6%"><span>编号</span></th>
                    <th width="10%"><span>二维码分类</span></th>
                    <th width="13%"><span>参数区间（起始）</span></th>
                    <th width="13%"><span>参数区间（结束）</span></th>
                    <th width="30%"><span>备注</span></th>
                    <th width="8%"><span>添加者</span></th>
                    <th width="10%"><span>添加时间</span></th>
                    <th width="12%"><span>操作</span></th>
                </tr>
                <tbody id="dvList">
                </tbody>
            </table>
            <div class="manage_page" id="manage_page">
            </div>
        </div>
        <!--manage_bottom end-->
    </div>
</div>
<!--manage end-->
<script type="text/javascript">
    var weixinid = "";
    var pageIndex = 1;
    var pageSize = 10;
    var pageCount = 1;
    var totalCount = 0;
    $(document).ready(function () {
        createMemu();
        $("#btnSave").click(function () {
            if (ischeck()) {
                var id = $("#hidid").val();
                var name = $("#name").val();
                var begin_val = $("#begin_val").val();
                var end_val = $("#end_val").val();
                var memo = $("#memo").val();
                if (id > 0) {
                    operate("2", id, name, begin_val, end_val, memo);
                }
                else {
                    operate("1", "0", name, begin_val, end_val, memo);
                }
            }
        });
        $("#btnRet").click(function () {
            reseting();
        });
    });
    function getContentMemu() {
        $(".manage_top_ul .manage_top_A").each(function () {
            weixinid = $(this).attr("typeid");
        })
        if (weixinid > 0) {
            gotoPage();
        }
    }
    //创建labs
    function createMemu() {
        $.ajax({
            type: "get",
            url: "@ST.GetTrendsFile("BaseConfiguration/WeixinUserMemu")" + "?random=" + Math.random(),
            async: true,
            data: { weixinid: "@ViewBag.weixinid" },
            dataType: "html",
            success: function (result) {
                $(".manage_top_ul").html(result);
                $(".manage_top_ul li").click(function () {
                    $(".manage_top_ul li").removeClass("manage_top_A");
                    $(this).addClass("manage_top_A");
                    reseting();
                    getContentMemu();
                })
                getContentMemu();
            }
        });
    }
    function ischeck() {
        var name = $("#name").val();
        if (name == "") {
            alert("分类不能空，请重新输入！");
            $("#name").focus();
            return false;
        }
        if (name.length > 10)
        { alert("分类长度不能超过10，请重新输入！"); $("#name").focus(); return false; }

        var beginval = $("#begin_val").val();
        if (beginval == "")
        { alert("参数开始值不能空，请重新输入！"); $("#begin_val").focus(); return false; }
        var begin = 0;
        try {
            begin = parseInt(beginval);
        }
        catch (err) {
            alert("参数值格式不正确，请重新输入！"); $("#begin_val").focus(); return false;
        }
        if (!(begin >= 0 && begin < 100000))
        { alert("参数值应在0-100000，请重新输入！"); $("#begin_val").focus(); return false; }


        var endval = $("#end_val").val();
        if (endval == "")
        { alert("参数开始值不能空，请重新输入！"); $("#end_val").focus(); return false; }
        var end = 0;
        try {
            end = parseInt(endval);
        }
        catch (err) {
            alert("参数值格式不正确，请重新输入！"); $("#end_val").focus(); return false;
        }
        if (!(end >= 0 && end < 100000))
        { alert("参数值应在0-100000，请重新输入！"); $("#end_val").focus(); return false; }

        if (begin > end)
        { alert("开始值不能大于结束值，请重新输入！"); $("#begin_val").focus(); return false; }

        var memo = $("#memo").val();
        if (memo.length > 100)
        { alert("备注长度不能超过100，请重新输入！"); $("#memo").focus(); return false; }
        return true;
    }

    var ajaxdata = {};
    function gotoPage() {
        ajaxdata["weixinid"] = weixinid;
        ajaxdata["pindex"] = pageIndex;
        ajaxdata["psize"] = pageSize;
        $.ajax({
            url: "@ST.GetTrendsFile("TwoCode/QueryTwoCodeType")",
            type: "POST",
            async: true,
            data: ajaxdata,
            beforeSend: function () {
                //showLoading();
            },
            complete: function () {
                //iddenLoading();
            },
            success: function (data) {
                if (data != "" && data != null && data != undefined) {
                    var arrData = data.split("~");
                    totalCount = arrData[0];
                    data = arrData[1];
                    var d = eval(data);
                    var html = "";
                    if (d != "" && d != null && d != undefined) {
                        for (var i = 0; i < d.length; i++) {
                            html += "<tr>";
                            html += getTxtTd(d[i]["ID"]) + getTxtTd(d[i]["name"]) + getTxtTd(d[i]["begin_val"]);
                            html += getTxtTd(d[i]["end_val"]) + getTxtTd(d[i]["memo"]) + getTxtTd(d[i]["username"]);
                            html += getTxtTd(d[i]["updatetime"].replace(/T/g, " "));
                            html += "<td><a class=\"manage_bottom_font blue\" onclick=\"selected('" + d[i]["ID"] + "','" + d[i]["name"] + "','" + d[i]["begin_val"] + "','" + d[i]["end_val"] + "','" + d[i]["memo"] + "')\" style='display:@(new EmoneyWeiXin.Controllers.BaseConfigurationController().DisplayOperate(3,ViewBag.leftid))'>编辑</a>";
                            html += "<a class=\"manage_bottom_font red\" onclick=\"operate('-1','" + d[i]["ID"] + "')\" style='display:@(new EmoneyWeiXin.Controllers.BaseConfigurationController().DisplayOperate(4,ViewBag.leftid))'>删除</a></td>";
                            html += '</tr>';
                        }
                    }
                    $("#dvList").html(html);
                    PageCountHtml();
                }
                else {
                    $("#dvList").html("没有查询到相关数据！");
                }
            },
            error: function (jqXHR, textStatus, errorThrown) {
                //alert(errorThrown);
            }
        });
    }
    function getTxtTd(s) {
        return '<td><span>' + s + '</span></td>';
    }

    //编辑，删除，关闭操作
    function operate(op, id, name, begin_val, end_val, memo) {
        $.ajax({
            url: "@ST.GetTrendsFile("TwoCode/UpdateTwoCodeType")",
            type: "POST",
            async: true,
            data: { op: op, id: id, name: name, beginval: begin_val, endval: end_val, memo: memo, weixinid: weixinid },
            beforeSend: function () {
                //showLoading();
            },
            complete: function () {
                // hiddenLoading();
            },
            success: function (data) {
                if (data == "1") {
                    gotoPage();
                    reseting();
                    alert("操作成功!");

                } else alert(data);
            },
            error: function (jqXHR, textStatus, errorThrown) {
                //alert(errorThrown);
            }
        });
    }
    //重置
    function reseting() {
        $("#hidid").val("0");
        $("#name").val("");
        $("#begin_val").val("");
        $("#end_val").val("");
        $("#memo").val("");
    }
    //选择行
    function selected(id, name, begin_val, end_val, memo) {
        $("#hidid").val(id);
        $("#name").val(name);
        $("#begin_val").val(begin_val);
        $("#end_val").val(end_val);
        $("#memo").val(memo);
    }
    //翻页
    function PageCountHtml() {
        pageCount = Math.ceil(totalCount / pageSize);
        if (pageCount == 0) {
            pageCount = 1;
        }
        $("#manage_page").html(PageHtml(pageIndex, pageCount));
        $("#manage_page .manage_page_right").click(function () {
            pageIndex = parseInt($("#txtPageNum").val()) + parseInt($(this).attr("tname"));
            $("#manage_page").html(PageHtml(pageIndex, pageCount));
            gotoPage();
        });
        $("#manage_page .tiaozhuan").click(function () {
            var pageNum = $("#txtPageNum").val();
            try {
                pageIndex = parseInt(pageNum);
            } catch (e) {
                alert("请输入数字");
                return false;
            }

            if (pageNum > 0 && pageNum <= pageCount) {
                $("#manage_page").html(PageHtml(pageIndex, pageCount));
                gotoPage();
            }
            else {
                alert("当前数字超出范围");
                return false;
            }
        });
    }
</script>

